<?php

namespace Taxx\KernelBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Taxx\KernelBundle\Model\Utils\PasswordUtils;
use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpFoundation\Session;
/**
 * Taxx\KernelBundle\Entity\User
 *
 * @ORM\Table(name="user")
 * @ORM\Entity
 */
class User 
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string $username
     *
     * @ORM\Column(name="username", type="string", length=50, nullable=false)
     */
    private $username;

    /**
     * @var Language
     *
     * @ORM\ManyToOne(targetEntity="Language")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="language_id", referencedColumnName="id")
     * })
     */
    private $language;
    
    /**
     * @var string $email
     *
     * @ORM\Column(name="email", type="string", length=50, nullable=false)
     */
    private $email;
            
    /**
     * @var string $password
     *
     * @ORM\Column(name="password", type="string", length=50, nullable=false)
     */
    private $password;

    /**
     * @var string $salt
     *
     * @ORM\Column(name="salt", type="string", length=10, nullable=false)
     */
    private $salt;
    
    /**
     * @var datetime $registered_at
     *
     * @ORM\Column(name="registered_at", type="datetime", nullable=false)
     */
    private $registered_at;
    
    /**
     * @var string $confirmation_code
     *
     * @ORM\Column(name="confirmation_code", type="string", length=20, nullable=false)
     */
    private $confirmation_code;
    
    /**
     * @var integer $active
     *
     * @ORM\Column(name="active", type="integer", nullable=false)
     */
    private $active;
    
    public function __construct($params=null, EntityManager $em=null)
    {
        if($params!=null)
        {
            if(isset($params['username']))
                $this->setUsername ($params['username']);
            if(isset($params['email']))
                $this->setEmail ($params['email']);
            if(isset($params['password']))
            {
                $salt = PasswordUtils::getSalt();
                $this->setSalt($salt);
                $password = PasswordUtils::generatePassword($params['password'], $salt);
                $this->setPassword ($password);
            }
            if(isset($params['language']))
            {
                $language = $em->getRepository('TaxxKernelBundle:Language')->find($params['language']);
                $this->setLanguage($language);
            }
            $this->setRegisteredAt(new \DateTime());
            $this->setConfirmationCode(substr(md5(PasswordUtils::getSalt()), 0, 20));
            $this->setActive(0);
        }
    }
    
    public function getId() {
        return $this->id;
    }

    public function setId($id) {
        $this->id = $id;
    }

    public function getUsername() {
        return $this->username;
    }

    public function setUsername($username) {
        $this->username = $username;
    }

    public function getLanguage() {
        return $this->language;
    }

    public function setLanguage($language) {
        $this->language = $language;
    }
    
    public function getEmail() {
        return $this->email;
    }

    public function setEmail($email) {
        $this->email = $email;
    }

    public function getPassword() {
        return $this->password;
    }

    public function setPassword($password) {
        $this->password = $password;
    }

    public function getSalt() {
        return $this->salt;
    }

    public function setSalt($salt) {
        $this->salt = $salt;
    }
        
    public function getRegisteredAt() {
        return $this->registered_at;
    }

    public function setRegisteredAt($registered_at) {
        $this->registered_at = $registered_at;
    }
    
    public function getConfirmationCode() {
        return $this->confirmation_code;
    }

    public function setConfirmationCode($confirmation_code) {
        $this->confirmation_code = $confirmation_code;
    }

    public function getActive() {
        return $this->active;
    }

    public function setActive($active) {
        $this->active = $active;
    }


}

?>